package com.lx.algorithm.code;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * Description:
 * Copyright:   Copyright (c)2019
 * Company:     zefu
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2022-01-01 15:44:19
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2022-01-01     张李鑫                     1.0         1.0 Version
 */
public class Code12 {
    /**
     * 如果给你一个二维数组，每一个值表示这一块地形的高度，
     * 求整块地形能装下多少水。
     */

    public static class Node implements Comparator<Node> {
        public int val;
        public int col;
        public int row;

        public Node(int val, int col, int row) {
            this.val = val;
            this.col = col;
            this.row = row;
        }

        @Override
        public int compare(Node o1, Node o2) {
            return o1.val - o2.val;
        }
    }

    public static int maxWaterII(int[][] arr) {
        //把外面一圈的数字都放到小根堆里去
        PriorityQueue<Node> queue = new PriorityQueue<>();
        boolean[][] isUse = new boolean[arr.length][arr[0].length];
        int max = Integer.MIN_VALUE;
        int sum = 0;
        //第一行
        for (int i = 0; i < arr[0].length; i++) {
            isUse[0][i] = true;
            queue.add(new Node(arr[0][i], 0, i));
        }

        //最后一行
        for (int i = 0; i < arr[arr.length - 1].length; i++) {
            isUse[arr.length - 1][i] = true;
            queue.add(new Node(arr[arr.length - 1][i], arr.length - 1, i));
        }

        //左边列
        for (int i = 0; i < arr.length; i++) {
            isUse[i][0] = true;
            queue.add(new Node(arr[i][0], i, 0));
        }

        //右边列
        for (int i = 0; i < arr.length; i++) {
            isUse[i][arr[0].length - 1] = true;
            queue.add(new Node(arr[i][arr[0].length - 1], i, arr[0].length - 1));
        }


        while (!queue.isEmpty()) {
            Node poll = queue.poll();
            max = Math.max(poll.val, max);
            //上面
            if (poll.col > 0 && !isUse[poll.col - 1][poll.row]) {
                sum += Math.max(max - arr[poll.col - 1][poll.row], 0);
                isUse[poll.col - 1][poll.row] = true;
                queue.add(new Node(arr[poll.col - 1][poll.row], poll.col - 1, poll.row));
            }
            //下面
            if (poll.col < arr.length - 1 && !isUse[poll.col + 1][poll.row]) {
                isUse[poll.col + 1][poll.row] = true;
                sum += Math.max(max - arr[poll.col + 1][poll.row], 0);
                queue.add(new Node(arr[poll.col + 1][poll.row], poll.col + 1, poll.row));
            }

            //右边
            if (poll.row < arr[0].length - 1 && !isUse[poll.col][poll.row + 1]) {
                isUse[poll.col][poll.row + 1] = true;
                sum += Math.max(max - arr[poll.col][poll.row + 1], 0);
                queue.add(new Node(arr[poll.col][poll.row + 1], poll.col, poll.row + 1));
            }

            if (poll.row > 0 && !isUse[poll.col][poll.row - 1]) {
                isUse[poll.col][poll.row - 1] = true;
                sum += Math.max(max - arr[poll.col][poll.row - 1], 0);
                queue.add(new Node(arr[poll.col][poll.row - 1], poll.col, poll.row - 1));
            }
        }
        return sum;
    }
}